*IAT SCORING ALGORITHM FROM GREENWALD, NOSEK, & BANAJI, 2003
LAST UPDATE TO MACRO: 05/27/05

--------------------------------------
Most recent changes:
- 5/27/05: Macro will now output the number of trials used to calculate the mean for each block
- 5/27/05: Lots of updating to the introductory and explanatory text
- 5/14/05: Macro will now analyze non-critical blocks (B3, B4, B6, B7) such as single dimension 
           practice trial data and output their means, percent error, and percent fast responses
- 5/14/05: Setting SUBEXCL to '2' (missing data) only occurs if there is missing data from the
           four critical analysis blocks
--------------------------------------

This macro will transform a datafile with raw trial latencies (stored as one line per response) 
for a standard format IAT (7 blocks) into a one line summary per subject of the IAT effect using
GNB's new scoring algorithm. The goal of this macro is to prepare IAT data for subsequent analysis.
However, this does not relieve the researcher from making conceptual decisions about how best to
analyze IAT data.  There are decisions to make about how the macro is applied, and the macro 
does not remove participants.  All subject exclusions must be made deliberately by the researcher.

To use this algorithm for your SAS program, perform these steps:  
(1) Run this script (do not edit the macro directly).  The macro will be loaded into active 
    memory and can be referred to in any analysis script
(2) Turn the datafile containing your IAT data into a SAS datafile and put it into folder to
    use as a library
(3) Identify that folder as a library and a folder where the SAS file output from the macro 
    will go (they can be the same folder)

EXAMPLE (without semicolons):
libname web 'H:\raceatt\'
libname outdata 'H:\raceattclean\'

(4) Prepare your SAS datafile to be used by the SAS Macro.  The critical elements are:
    (a) If there are multiple IATs per participant,
        then the macro will need to be run for each IAT individually and the data can be 
        merged manually afterwards
    (b) the dataset must contain one row per IAT trial
    (c) the following variables must be available in each row: subject identifier, trial latency,
        trial error (0=correct, 1=error), name of block = B3, B4, B6, B7 (corresponding to the
        3,4,6,7 blocks in the standard 7-block IAT format)
    (d) the macro has no idea what task subjects were performing in the blocks, it just 
        calculates the performance difference average(6-3, 7-4), you are responsible for 
        knowing what those blocks refer to for each participant

EXAMPLE (without semicolons): BLOCK_NAME is an existing variable defining what is in each block,
BLOCK is the new variable that will be passed to the macro.
     if BLOCK_NAME in ('goodbad') then BLOCK = 'B1 ' 
	 else if BLOCK_NAME in ('bushkerry') then BLOCK = 'B2'
     else if BLOCK_NAME in ('bushgoodpractice') then BLOCK = 'B3'
	 else if BLOCK_NAME in ('bushgoodcritical') then BLOCK = 'B4'
     else if BLOCK_NAME in ('kerrybush') then BLOCK = 'B5'
	 else if BLOCK_NAME in ('bushbadpractice') then BLOCK = 'B6'
     else if BLOCK_NAME in ('bushbadcritical') then BLOCK = 'B7'

Note: the IAT score will be based on B3, B4, B6, B7 only.  If other blocks are included as in 
this example, the macro will calculate basic statistics: mean, error rate, number, fast responses.

(5) In your SAS program, enter the following statement 

%iatCalc(libIn, libOut, INDATA, OUTDATA, BLOCNAME, SESHID, TRIAL_LATENCY, TRIAL_ERROR, VERROR, VEXTREME, VSD)

(6) the variable names between the parantheses are placeholders, you will change those values
    to ones that correspond with the file and variable names in your own datafile, and the last
    three (VERROR, VEXTREME, VSD) will be replaced with numerical values (1 or 2) according to 
    the definitions described below

EXAMPLE (without semicolons):
%iatCalc(web, outdata, iatrace, CLEANiat, BLOCK, SUB, LATENCY, ERROR, 1, 2,1)

(7) run the script
(8) examine datafile to find your calculated IAT scores (in the example, the file would be called
    outdata.CLEANiat
(9) the macro does not remove individual participants, it only identifies IAT scores that are
    clearly problematic.  The SUBEXCL variable is 0 if the IAT performance passes basic standards,
    1 if more than 10% of trials in the four main blocks were <300ms, 2 if data from a main block
    was missing.  Individual subjects will need to be removed by the researcher, and any additional
    exclusion criteria will have to be defined and implemented by the researcher.  The macro is not 
    designed to make conceptual decisions about data inclusion except for the most conservative
    criteria.

Descriptions of what the macro expects for input, and what it will output are below.  

The macro expects the following types of libraries, datafiles, and data:

(a) variables identifying SAS library and filenames 
      libIn     = input SAS library name
      libOut    = output SAS library name
      INDATA    = filename of input SAS dataset in the input SAS library
      OUTDATA   = filename for the output SAS dataset in the output SAS library

(b) variables identifying four key pieces of information for calculating an IAT score
      BLOCNAME  = variable name for block identifier in the indata file: alphanumeric indication of the 
                  four trial blocks ('B3', 'B4', 'B6', 'B7' are critical blocks corresponding 
                  to B3, B4, B6, and B7 from GNB, 2002).  At present the macro requires that the variable 
                  passed here uses the names 'B3', 'B4', 'B6', 'B7' to refer to the B3-B7 blocks
	  SESHID    = variable name for unique subject identifier in the indata file
	  TRIAL_LATENCY  = variable name for latency of response for trial in the indata file
	  TRIAL_ERROR  = variable name for error coding: 0 if initial response was correct, 1 if initial response 
                  was incorrect in the indata file

(c) three options for variations in the D algorithm
      VERROR    = value: if '1' the algorithm will use error trial latencies, if '2' the algorithm will replace 
                  error trial latencies with blockmean+600 [blockmean is mean of correct responses only]
                  (1 is current standard for designs that require error correction, 2 if error correction is
                  not required)
      VEXTREME  = value: if '1' the algorithm provides no treatment of extreme values, if '2' the algorithm
                  will delete trials <400ms (2 is current standard)
      VSTD      = value: if '1' the block standard deviation is performed including error trials (corrected or not),
                  if '2' the block standard deviation is performed on correct responses only (1 is standard)

Note: The D algorithm is not the definitive scoring method for the IAT.  Improvements will be 
identified with continuing research by the academic community.  This macro conservatively 
applies the best algorithms identified by Greenwald et al., 2003.  Further enhancements to that 
algorithm will need to be validated and applied separately from this script.  The script itself 
will evolve more slowly than innovations in scoring to ensure that the validity of its procedures 
is well documented prior to their standardization.

The macro will output the following variables to a new file identified by the OUTDATA variable,
if there is an existing file by the named used for OUTDATA, the macro will overwrite it:
      SESHID  = unique subject identifier
      SUBEXCL = 0 for inclusion data, 1 for excluded data, 2 for incomplete data
      MB3     = mean of trial latencies for B3
      MB4     = mean of trial latencies for B4
      MB6     = mean of trial latencies for B6
      MB7     = mean of trial latencies for B7
      CS1     = standard deviation for B3 and B6 trials combined (correct trials only)
      CS2     = standard deviation for B4 and B7 trials combined (correct trials only)
      AS1     = standard deviation for B3 and B6 trials combined (all trials)
      AS2     = standard deviation for B4 and B7 trials combined (all trials)
      EB3     = percent errors of trials for B3
      EB4     = percent errors of trials for B4
      EB6     = percent errors of trials for B6
      EB7     = percent errors of trials for B7
      NB3     = number of trials used for mean calculation for B3
      NB4     = number of trials used for mean calculation for B4
      NB6     = number of trials used for mean calculation for B6
      NB7     = number of trials used for mean calculation for B7
      FB3     = percent fast responses of trials for B3
      FB4     = percent fast responses of trials for B4
      FB6     = percent fast responses of trials for B6
      FB7     = percent fast responses of trials for B7
      DIFF1   = MB6 - MB3
      DIFF2   = MB7 - MB4
      IAT1    = DIFF1/STD1
      IAT2    = DIFF2/STD2
	  IAT     = mean of IAT1 and IAT2

Note: The macro will also output M (mean), E (error), N (number) and F (fast) variables for all
other blocks included in the initial SAS datafile.  Those additional calculations will not affect 
the IAT score.  If you do not want values calculated for other blocks, remove them from the 
input datafile before invoking the script.
;

/*
MACROS for NEW IMPLICIT ASSOCIATION TEST SCORING ALGORITHM (Greenwald, Nosek, & Banaji, 2003)

FOR STANDARD USE, DO NOT CHANGE THE MACRO ITSELF.  JUST RUN THE MACRO AND CALL IT BY FOLLOWING
THE INSTRUCTIONS ABOVE.
*/
%macro iatCalc(libIn, libOut, indata, outdata, BLOCNAME, SESHID, TRIAL_LATENCY, TRIAL_ERROR, VERROR, VEXTREME, VSTD);
%let divide= /; %let multiply= *; %let add = +; %let subtract = -;
%iatAlgorithm(libIn=&libIn, libOut=&libOut, indata=&indata, outdata=&outdata, BLOCNAME=&BLOCNAME, 
              SESHID=&SESHID, TRIAL_LATENCY=&TRIAL_LATENCY, TRIAL_ERROR=&TRIAL_ERROR, VERROR=&VERROR, VEXTREME=&VEXTREME, VSTD=&VSTD);run;
%mend iatCalc;

%macro iatAlgorithm(libIn, libOut, indata, outdata, BLOCNAME, SESHID, TRIAL_LATENCY, TRIAL_ERROR, VERROR, VEXTREME, VSTD);
data IAT; set &libIn..&indata; 
%*PRELIMINARY STEPS FOR HANDLING WEBDATA FORMATS;
    keep &BLOCNAME &SESHID &TRIAL_LATENCY &TRIAL_ERROR;
proc sort data=iat; by &SESHID &BLOCNAME;	
	%*options nonotes; %*suppress all Notes to log;	

data IAT; set IAT;

%*STEP 1 HAS BEEN REMOVED, NOW ALL DATA IS AT LEAST PARTIALLY ANALYZED;
%*STEP 1: Include data from B3, B4, B6, B7;
   %*if &BLOCNAME in ('B3', 'B4', 'B6', 'B7') then ;
   %*else delete;

%*STEP 2a: Eliminate trial latencies > 10,000ms;
   if &TRIAL_LATENCY > 10000 then delete;

%*STEP 2b: Eliminate subjects for whom more than 10% of trials have latencies < 300ms;
   else if &TRIAL_LATENCY < 0 then delete; %*for miscoded data in datafile indicating negative response times;
   else if -1 < &TRIAL_LATENCY < 300 then FAST = 1;
   else FAST = 0;

data FASTDATA; set IAT; keep &SESHID &BLOCNAME FAST;
proc sort; by &SESHID &BLOCNAME;
proc means data=IAT noprint; by &SESHID &BLOCNAME; var FAST; output out=means mean=MEAN;
proc transpose data=means prefix=F name=name out=FASTMEAN; by &SESHID; id &BLOCNAME;
data FASTMEAN; set FASTMEAN; where name='MEAN'; FASTM = mean(FB3, FB4, FB6, FB7);
   if FASTM > .10 then SUBEXCL = 1; else SUBEXCL = 0; 
   %*SUBEXCL = 0 (include data), 1 (exclude data - too many fast responses), 2 (exclude data - missing data);
   %*The SUBEXCL variable needed to be reintroduced to the final dataset in STEP 12;

%*STEP 3: Use all trials;
   %*in the conventional algorithm the first two trials of each block would be dropped here;

%*STEP 4: No extreme value treatment <or> delete trial with latencies <400ms;
data IAT; set IAT; 
    %*if &VEXTREME = 1 then do nothing here;
    if &VEXTREME = 2 then do; if &TRIAL_LATENCY < 400 then delete; end;

proc sort data=IAT; by &SESHID &BLOCNAME;

%*STEP 5: Compute mean of correct latencies for each block;
data CORR; set IAT;
   %*if &VERROR is 1 then means and SDs will be calculated for the entire set of latencies;
   if &VERROR = 2 then do; if &TRIAL_ERROR NE 0 then delete; end;
   keep &SESHID &BLOCNAME &TRIAL_LATENCY &TRIAL_ERROR;
proc means data=CORR noprint; by &SESHID &BLOCNAME; var &TRIAL_LATENCY; output out=means mean=MEAN;
proc transpose data=means prefix=M name=name out=CORRMEAN; by &SESHID; id &BLOCNAME;
data CORRMEAN; set CORRMEAN; where name='MEAN';

%*STEP 5x: Count number of trials used for each block mean calculation;
proc means data=CORR noprint; by &SESHID &BLOCNAME; var &TRIAL_LATENCY; output out=means n=N; 
proc transpose data=means prefix=N name=name out=NMEAN; by &SESHID; id &BLOCNAME;
data NMEAN; set NMEAN; where name='N';run; 
*outputs a file with the number of actual trials in each block;

%*STEP 6a: Compute pooled SD for B3 & B6, and separately for B4 & B7 for correct trials only;
data SD; set CORR;
   if &TRIAL_ERROR NE 0 then delete;  *drop error trials;
   if &BLOCNAME in ('B3', 'B6') then TD = '1';
   else if &BLOCNAME in ('B4', 'B7') then TD = '2';
   else delete;
   drop &BLOCNAME;
proc sort data=SD; by &SESHID TD;
proc means data=SD noprint; by &SESHID TD; var &TRIAL_LATENCY; output out=means std=STD;
proc transpose data=means prefix=CS name=name out=CORRSTD2; by &SESHID; id TD;
data CORRSTD2; set CORRSTD2; where name='STD';

%*STEP 6b: Compute pooled SD for B3 & B6, and separately for B4 & B7 including error trials;
data SD; merge IAT CORRMEAN; by &SESHID;
   if &TRIAL_ERROR < 0 then delete;
   else if &TRIAL_ERROR > 1 then delete; %*get rid of coding errors;
   else if &TRIAL_ERROR = 1 and &VERROR = 2 then do;
      if &BLOCNAME in ('B3') then &TRIAL_LATENCY = MB3 + 600;
	  else if &BLOCNAME in ('B4') then &TRIAL_LATENCY = MB4 + 600;
	  else if &BLOCNAME in ('B6') then &TRIAL_LATENCY = MB6 + 600;
	  else if &BLOCNAME in ('B7') then &TRIAL_LATENCY = MB7 + 600;
   end;
   if &BLOCNAME in ('B3', 'B6') then TD = '1';
   else if &BLOCNAME in ('B4', 'B7') then TD = '2';
   else delete;
   drop &BLOCNAME;
proc sort data=SD; by &SESHID TD;
proc means data=SD noprint; by &SESHID TD; var &TRIAL_LATENCY; output out=means std=STD;
proc transpose data=means prefix=AS name=name out=CORRSTD1; by &SESHID; id TD;
data CORRSTD1; set CORRSTD1; where name='STD';

%*STEP 7: Replace error latencies with block mean + 600ms 
<or> use latency from stimulus onset to correct response (when correct response is required);
data ERR; set IAT;
   keep &SESHID &BLOCNAME &TRIAL_ERROR;
   if &TRIAL_ERROR < 0 then delete;
   else if &TRIAL_ERROR > 1 then delete; %*get rid of coding errors;
proc means data=ERR noprint; by &SESHID &BLOCNAME; var &TRIAL_ERROR; output out=means mean=MEAN;
proc transpose data=means prefix=E name=name out=ERRMEAN; by &SESHID; id &BLOCNAME;
data ERRMEAN; set ERRMEAN; where name='MEAN';

%*STEP 7 continued: combining data;
data COMBINE; merge CORRMEAN NMEAN CORRSTD1 CORRSTD2 ERRMEAN FASTMEAN; by &SESHID; %*combining datasets for calculating final means;
   if &VERROR=2 then do;
      array BLOCKMeans(*) MB: ;
      array BLOCKCstds(*) CS: ;
	  array BLOCKAstds(*) AS: ;
      array BLOCKErrs(*) EB: ;
      do i=1 to dim(BLOCKMeans); %*for each of the four blocks replace error trials with mean + 600ms;
	     BLOCKMeans{i} = (1-BLOCKErrs{i})*BLOCKMeans{i} + (BLOCKErrs{i})*(BLOCKMeans{i}+600);			
      end;
   end;

%*STEP 8: No transformation of latencies;
    %*in the conventional algorithm, raw latencies would be log transformed prior to the transposing in the current format;

%*STEP 9: Average latencies for each of the four blocks;
    %*this step was already accomplished in the do loop above;

%*STEP 10: Compute two differences B6-B3 and B7-B4 (does not account for pairing order);
   DIFF1 = MB6 - MB3;
   DIFF2 = MB7 - MB4;

%*STEP 11: Divide each difference by associated pooled SD from STEP 6a or 6b;
   If &VSTD = 2 then do;
      IAT1 = DIFF1/CS1;
      IAT2 = DIFF2/CS2;
   end;
   else do; %*IF VSTD = 1 also set as default;
      IAT1 = DIFF1/AS1;
	  IAT2 = DIFF2/AS2;
   end; 

%*STEP 12: Average quotients from STEP 11;
   IAT = mean(IAT1, IAT2);

%*if there is missing data in critical blocks, mark data as excluded (SUBEXCL=2);
                               %* old way of doing it  do i=1 to dim(BLOCKMeans);
                               %*  if BLOCKMeans{i} = . then SUBEXCL = 2;
                               %*end;
   if MB3=. | MB4=. | MB6=. | MB7=. then SUBEXCL=2; *if missing data from any of critical blocks then marked;

data &libout..&outdata (drop=i name);
	set COMBINE;
run;
%mend iatAlgorithm;
*END OF ALGORITHM;

**************
Standard Analysis Program for web demo datasets - gender-science task
Fred Smyth
Last Updated: 31 May 2006
**************;

libname web 'C:\primary\DataWeb\RESEARCH.Studies\Mathbalance\datastorage\';  *identify library where processed files will be stored;  

data web.acasession; set web.sessionacapref30000 web.sessionacapref60000;
     creation_date = madedate; session_id = seshid; 
	 session_status = seshstat; study_name = stdyname;
	 last_update_date = updtdate;
	 drop madedate seshid seshstat stdyname updtdate;
data web.acaiat; set web.iatacapref30000 web.iatacapref60000;
     block_name = blocname; block_pairing_definition = bpairdef; block_number = blocknum;
	 block_trial_count = btrlcnt; session_id = seshid; study_name = stdyname; 
	 task_number = tasknum; trial_response = trespons; trial_error = trlerror;
	 trial_latency = trllatnc; trial_name = trlname; trial_number = trlnum;
	 drop BLOCNAME BLOCNUM BPAIRDEF BTRLCNT SESHID STDYNAME TASKNUM TRESPONS 
          TRLERROR TRLLATNC TRLNAME TRLNUM;
data web.acaexplicit; set web.explicitacapref30000 web.explicitacapref60000;
     question_name = qname; questionnaire_name = qnrname; question_number = qnum;
	 question_response = qrespons; session_id = seshid; study_name = stdyname;
	 task_number = tasknum;
	 drop QNAME QNRNAME QNUM QRESPONS SESHID STDYNAME TASKNUM;

TITLE 'DATA = academic attitudes';
ODS HTML BODY = 'C:\primary\DataWeb\RESEARCH.Studies\Mathbalance\datastorage\combine.01.sessionStatus.html';
  proc means n mean std data = web.acasession; class session_status; var session_id; run;
ODS HTML CLOSE;

data temp; set web.acaiat; 
     if BLOCK_NAME in ('BLOCK0', 'BLOCK1', 'BLOCK4') then delete;
	 else if BLOCK_NAME in ('BLOCK2') then BLOCK = 'B3';
	 else if BLOCK_NAME in ('BLOCK3') then BLOCK = 'B4';
	 else if BLOCK_NAME in ('BLOCK5') then BLOCK = 'B6';
	 else if BLOCK_NAME in ('BLOCK6') then BLOCK = 'B7';

	 /*correcting a coding error*/
     if block_pairing_definition in ('MATH/Anxious,ARTS/Confident') and block in ('B3','B4') then task_name = 'Amathanxa';
     else if block_pairing_definition in ('MATH/Anxious,VERBAL/Confident') and block in ('B3','B4') then task_name = 'Vmathanxa';
     else if block_pairing_definition in ('MATH/Anxious,FURNITURE/Confident') and block in ('B3','B4') then task_name = 'Fmathanxa';
     else if block_pairing_definition in ('ARTS/Anxious,MATH/Confident') and block in ('B6','B7') then task_name = 'Amathanxa';
     else if block_pairing_definition in ('VERBAL/Anxious,MATH/Confident') and block in ('B6','B7') then task_name = 'Vmathanxa';
     else if block_pairing_definition in ('FURNITURE/Anxious,MATH/Confident') and block in ('B6','B7') then task_name = 'Fmathanxa';

	 drop block_number; *empty;

/**********************************************************************************************/
/* DUPLICATE DATA BY ERROR */
/**********************************************************************************************/
*sometimes data is inadvertently sent to the database twice for the same task;
*this section will sort the data, identify duplicates, and remove them;
 /*use the next line only if analyzing data which include multiple IATs by design*/
 /*proc sort data=temp; by session_id task_name_s block_name_s trial_number; */
proc sort data=temp; by session_id task_name block_name trial_number; 
/**********************************************************************************************/
      /*the preceding line should be used as the default when just one IAT
         is being analyzed in the script */
/**********************************************************************************************/
 data temp; set temp; repeat=0;
 /**********************************************************************************************/
/*use the next code only if analyzing data which include multiple IATs by design (this assumes 
     that the same task_name is not administered twice in a session)  */
 /*if session_id = lag(session_id) and task_name_s = lag(task_name_s)
        and block_name_s = lag(block_name_s) and trial_number = lag(trial_number) then repeat=1; */
 /**********************************************************************************************/
    if session_id = lag(session_id) and task_name = lag(task_name) and block_name = lag(block_name)
        and trial_number = lag(trial_number) then repeat=1;
	  /*the preceding line is more general, and should be used as the default when just one IAT 
		is being analyzed in the script */
TITLE 'DATA = academic attitudes';
ODS HTML BODY = 'C:\primary\DataWeb\RESEARCH.Studies\Mathbalance\datastorage\combine.02.duplicateIATlinesRemoved.html';
  proc freq; tables repeat; run; 
ODS HTML CLOSE;

data temp; set temp; if repeat = 1 then delete;  *dropping repeated data;
/**********************************************************************************************/

* Part 2 - Initial data cleaning checks - what versions of the IAT were used in this dataset?;

*number of blocks;
TITLE 'DATA = academic attitudes';
ODS HTML BODY = 'C:\primary\DataWeb\RESEARCH.Studies\Mathbalance\datastorage\combine.03.blockFREQ.html';
  proc means n mean min max; class block_name; var session_id; where trial_number = 1;run;  
ODS HTML CLOSE;

*trials per block;
TITLE 'DATA = academic attitudes';
ODS HTML BODY = 'C:\primary\DataWeb\RESEARCH.Studies\Mathbalance\datastorage\combine.04.trialsPERblock.html';
 proc means n mean min max; class block_name block_trial_count; var session_id; where trial_number = 1;run; 
ODS HTML CLOSE;

*block pairings;
TITLE 'DATA = academic attitudes';
ODS HTML BODY = 'C:\primary\DataWeb\RESEARCH.Studies\Mathbalance\datastorage\combine.05.blockPairings.html';
  proc means n mean min max; class block_pairing_definition; var session_id; where trial_number = 1;run; 
ODS HTML CLOSE;

*stimulus items;
TITLE 'DATA = academic attitudes';
ODS HTML BODY = 'C:\primary\DataWeb\RESEARCH.Studies\Mathbalance\datastorage\combine.06.stimulusItems.html';
 proc means n mean min max; class trial_name; var session_id;run; 
ODS HTML CLOSE;

* It is likely that there will be some errant blocks and stimulus items from other tasks/studies because of miscoding;
* these will be eliminated in subsequent phases;

* Part 3 - Preparing the datafile for analysis with the SAS IAT macro for scoring;
data web.raw; set temp;
	 *only keep the variables relevant for subsequent analysis;
     keep TRIAL_NUMBER BLOCK SESSION_ID STUDY_NAME BLOCK_PAIRING_DEFINITION TRIAL_LATENCY TRIAL_ERROR TASK_NAME task_number;

data web.mathatt; set web.raw;
  if TASK_NAME in ('Amathatta', 'Amathattb', 'Fmathatta', 'Fmathattb', 'Vmathatta', 'Vmathattb') then ; else delete;
%iatCalc(web, web, mathatt, mathatt, BLOCK, SESSION_ID, TRIAL_LATENCY, TRIAL_ERROR, 1, 2, 1);
data web.mathatt; set web.mathatt;
  Imathatt = IAT; Imathatt1 = IAT1; Imathatt2 = IAT2; MAexcl = subexcl; 
  MAmb3 = mb3; MAmb4 = mb4; MAmb6 = mb6; MAmb7 = mb7; 
  MAeb3 = eb3; MAeb4 = eb4; MAeb6 = eb6; MAeb7 = eb7;
  MAfb3 = fb3; MAfb4 = fb4; MAfb6 = fb6; MAfb7 = fb7;
  drop IAT IAT1 IAT2 subexcl mb3 mb4 mb6 mb7 eb3 eb4 eb6 eb7 fb3 fb4 fb6 fb7 diff1 diff2 as1 as2 cs1 cs2 fastm nb3 nb4 nb6 nb7;
DATA web.mathatt1; SET web.raw; 
  if TASK_NAME in ('Amathatta', 'Amathattb', 'Fmathatta', 'Fmathattb', 'Vmathatta', 'Vmathattb') then ; else delete;
  IF block IN ('B3','B6') THEN trial_number = trial_number+1; ELSE trial_number = trial_number+21;
      if block in ('B3','B4') AND 0 < trial_number < 16 THEN blockn = 'B3'; ELSE IF block in ('B3','B4') AND (trial_number > 15 AND trial_number < 31) THEN blockn = 'B4';
      ELSE IF block in ('B6', 'B7') AND 0 < trial_number < 16 THEN blockn = 'B6'; ELSE IF block in ('B6', 'B7') AND (trial_number > 15 AND trial_number < 31) THEN blockn = 'B7';
%iatCalc(web, web, mathatt1, mathatt1, blockn, session_id, trial_latency, trial_error, 1, 2, 1);
DATA web.mathatt1; SET web.mathatt1; Imathatta = IAT1; Imathattb = IAT2; KEEP session_id Imathatta Imathattb;
DATA web.mathatt2; SET web.raw;
  if TASK_NAME in ('Amathatta', 'Amathattb', 'Fmathatta', 'Fmathattb', 'Vmathatta', 'Vmathattb') then ; else delete;
  IF block IN ('B3','B6') THEN trial_number = trial_number+1; ELSE trial_number = trial_number+21;
      if block in ('B3','B4') AND 30 < trial_number < 46 THEN blockn = 'B3'; ELSE IF block in ('B3','B4') AND (trial_number > 45) THEN blockn = 'B4';
      ELSE IF block in ('B6', 'B7') AND 30 < trial_number < 46 THEN blockn = 'B6'; ELSE IF block in ('B6', 'B7') AND (trial_number > 45) THEN blockn = 'B7';
%iatCalc(web, web, mathatt2, mathatt2, blockn, session_id, trial_latency, trial_error, 1, 2, 1);
DATA web.mathatt2; SET web.mathatt2; Imathattc= IAT1; Imathattd= IAT2; KEEP session_id Imathattc Imathattd;
data web.mathatt; merge web.mathatt web.mathatt1 web.mathatt2; by session_id;


data web.mathid; set web.raw;
  if TASK_NAME in ('Amathida', 'Amathidb', 'Fmathida', 'Fmathidb', 'Vmathida', 'Vmathidb') then ; else delete; 
%iatCalc(web, web, mathid, mathid, BLOCK, SESSION_ID, TRIAL_LATENCY, TRIAL_ERROR, 1, 2, 1);
data web.mathid; set web.mathid;
  Imathid = IAT; Imathid1 = IAT1; Imathid2 = IAT2; MIexcl = subexcl; 
  MImb3 = mb3; MImb4 = mb4; MImb6 = mb6; MImb7 = mb7; 
  MIeb3 = eb3; MIeb4 = eb4; MIeb6 = eb6; MIeb7 = eb7;
  MIfb3 = fb3; MIfb4 = fb4; MIfb6 = fb6; MIfb7 = fb7;
  drop IAT IAT1 IAT2 subexcl mb3 mb4 mb6 mb7 eb3 eb4 eb6 eb7 fb3 fb4 fb6 fb7 diff1 diff2 as1 as2 cs1 cs2 fastm nb3 nb4 nb6 nb7;
DATA web.mathid1; SET web.raw; 
  if TASK_NAME in ('Amathida', 'Amathidb', 'Fmathida', 'Fmathidb', 'Vmathida', 'Vmathidb') then ; else delete;
  IF block IN ('B3','B6') THEN trial_number = trial_number+1; ELSE trial_number = trial_number+21;
      if block in ('B3','B4') AND 0 < trial_number < 16 THEN blockn = 'B3'; ELSE IF block in ('B3','B4') AND (trial_number > 15 AND trial_number < 31) THEN blockn = 'B4';
      ELSE IF block in ('B6', 'B7') AND 0 < trial_number < 16 THEN blockn = 'B6'; ELSE IF block in ('B6', 'B7') AND (trial_number > 15 AND trial_number < 31) THEN blockn = 'B7';
%iatCalc(web, web, mathid1, mathid1, blockn, session_id, trial_latency, trial_error, 1, 2, 1);
DATA web.mathid1; SET web.mathid1; Imathida = IAT1; Imathidb = IAT2; KEEP session_id Imathida Imathidb;
DATA web.mathid2; SET web.raw;
  if TASK_NAME in ('Amathida', 'Amathidb', 'Fmathida', 'Fmathidb', 'Vmathida', 'Vmathidb') then ; else delete;
  IF block IN ('B3','B6') THEN trial_number = trial_number+1; ELSE trial_number = trial_number+21;
      if block in ('B3','B4') AND 30 < trial_number < 46 THEN blockn = 'B3'; ELSE IF block in ('B3','B4') AND (trial_number > 45) THEN blockn = 'B4';
      ELSE IF block in ('B6', 'B7') AND 30 < trial_number < 46 THEN blockn = 'B6'; ELSE IF block in ('B6', 'B7') AND (trial_number > 45) THEN blockn = 'B7';
%iatCalc(web, web, mathid2, mathid2, blockn, session_id, trial_latency, trial_error, 1, 2, 1);
DATA web.mathid2; SET web.mathid2; Imathidc= IAT1; Imathidd= IAT2; KEEP session_id Imathidc Imathidd;
data web.mathid; merge web.mathid web.mathid1 web.mathid2; by session_id;


data web.mathanx; set web.raw;
  if TASK_NAME in ('Amathanxa', 'Amathanxb', 'Fmathanxa', 'Fmathanxb', 'Vmathanxa', 'Vmathanxb') then ; else delete; 
%iatCalc(web, web, mathanx, mathanx, BLOCK, SESSION_ID, TRIAL_LATENCY, TRIAL_ERROR, 1, 2, 1);
data web.mathanx; set web.mathanx;
  Imathanx = IAT; Imathanx1 = IAT1; Imathanx2 = IAT2; MXexcl = subexcl; 
  MXmb3 = mb3; MXmb4 = mb4; MXmb6 = mb6; MXmb7 = mb7; 
  MXeb3 = eb3; MXeb4 = eb4; MXeb6 = eb6; MXeb7 = eb7;
  MXfb3 = fb3; MXfb4 = fb4; MXfb6 = fb6; MXfb7 = fb7;
  drop IAT IAT1 IAT2 subexcl mb3 mb4 mb6 mb7 eb3 eb4 eb6 eb7 fb3 fb4 fb6 fb7 diff1 diff2 as1 as2 cs1 cs2 fastm nb3 nb4 nb6 nb7;
DATA web.mathanx1; SET web.raw; 
  if TASK_NAME in ('Amathanxa', 'Amathanxb', 'Fmathanxa', 'Fmathanxb', 'Vmathanxa', 'Vmathanxb') then ; else delete;
  IF block IN ('B3','B6') THEN trial_number = trial_number+1; ELSE trial_number = trial_number+21;
      if block in ('B3','B4') AND 0 < trial_number < 16 THEN blockn = 'B3'; ELSE IF block in ('B3','B4') AND (trial_number > 15 AND trial_number < 31) THEN blockn = 'B4';
      ELSE IF block in ('B6', 'B7') AND 0 < trial_number < 16 THEN blockn = 'B6'; ELSE IF block in ('B6', 'B7') AND (trial_number > 15 AND trial_number < 31) THEN blockn = 'B7';
%iatCalc(web, web, mathanx1, mathanx1, blockn, session_id, trial_latency, trial_error, 1, 2, 1);
DATA web.mathanx1; SET web.mathanx1; Imathanxa = IAT1; Imathanxb = IAT2; KEEP session_id Imathanxa Imathanxb;
DATA web.mathanx2; SET web.raw;
  if TASK_NAME in ('Amathanxa', 'Amathanxb', 'Fmathanxa', 'Fmathanxb', 'Vmathanxa', 'Vmathanxb') then ; else delete;
  IF block IN ('B3','B6') THEN trial_number = trial_number+1; ELSE trial_number = trial_number+21;
      if block in ('B3','B4') AND 30 < trial_number < 46 THEN blockn = 'B3'; ELSE IF block in ('B3','B4') AND (trial_number > 45) THEN blockn = 'B4';
      ELSE IF block in ('B6', 'B7') AND 30 < trial_number < 46 THEN blockn = 'B6'; ELSE IF block in ('B6', 'B7') AND (trial_number > 45) THEN blockn = 'B7';
%iatCalc(web, web, mathanx2, mathanx2, blockn, session_id, trial_latency, trial_error, 1, 2, 1);
DATA web.mathanx2; SET web.mathanx2; Imathanxc= IAT1; Imathanxd= IAT2; KEEP session_id Imathanxc Imathanxd;
data web.mathanx; merge web.mathanx web.mathanx1 web.mathanx2; by session_id;

data web.genid; set web.raw;
  if TASK_NAME in ('genida', 'genidb') then ; else delete; 
%iatCalc(web, web, genid, genid, BLOCK, SESSION_ID, TRIAL_LATENCY, TRIAL_ERROR, 1, 2, 1);
data web.genid; set web.genid;
  Igenid = IAT; Igenid1 = IAT1; Igenid2 = IAT2; GIexcl = subexcl; 
  GImb3 = mb3; GImb4 = mb4; GImb6 = mb6; GImb7 = mb7; 
  GIeb3 = eb3; GIeb4 = eb4; GIeb6 = eb6; GIeb7 = eb7;
  GIfb3 = fb3; GIfb4 = fb4; GIfb6 = fb6; GIfb7 = fb7;
  drop IAT IAT1 IAT2 subexcl mb3 mb4 mb6 mb7 eb3 eb4 eb6 eb7 fb3 fb4 fb6 fb7 diff1 diff2 as1 as2 cs1 cs2 fastm nb3 nb4 nb6 nb7;
DATA web.genid1; SET web.raw; 
  if TASK_NAME in ('genida', 'genidb') then ; else delete;
  IF block IN ('B3','B6') THEN trial_number = trial_number+1; ELSE trial_number = trial_number+21;
      if block in ('B3','B4') AND 0 < trial_number < 16 THEN blockn = 'B3'; ELSE IF block in ('B3','B4') AND (trial_number > 15 AND trial_number < 31) THEN blockn = 'B4';
      ELSE IF block in ('B6', 'B7') AND 0 < trial_number < 16 THEN blockn = 'B6'; ELSE IF block in ('B6', 'B7') AND (trial_number > 15 AND trial_number < 31) THEN blockn = 'B7';
%iatCalc(web, web, genid1, genid1, blockn, session_id, trial_latency, trial_error, 1, 2, 1);
DATA web.genid1; SET web.genid1; Igenida = IAT1; Igenidb = IAT2; KEEP session_id Igenida Igenidb;
DATA web.genid2; SET web.raw;
  if TASK_NAME in ('genida', 'genidb') then ; else delete;
  IF block IN ('B3','B6') THEN trial_number = trial_number+1; ELSE trial_number = trial_number+21;
      if block in ('B3','B4') AND 30 < trial_number < 46 THEN blockn = 'B3'; ELSE IF block in ('B3','B4') AND (trial_number > 45) THEN blockn = 'B4';
      ELSE IF block in ('B6', 'B7') AND 30 < trial_number < 46 THEN blockn = 'B6'; ELSE IF block in ('B6', 'B7') AND (trial_number > 45) THEN blockn = 'B7';
%iatCalc(web, web, genid2, genid2, blockn, session_id, trial_latency, trial_error, 1, 2, 1);
DATA web.genid2; SET web.genid2; Igenidc= IAT1; Igenidd= IAT2; KEEP session_id Igenidc Igenidd;
data web.genid; merge web.genid web.genid1 web.genid2; by session_id;


data web.mathstro; set web.raw;
  if TASK_NAME in ('Amathstroa', 'Amathstrob', 'Fmathstroa', 'Fmathstrob', 'Vmathstroa', 'Vmathstrob') then ; else delete;
%iatCalc(web, web, mathstro, mathstro, BLOCK, SESSION_ID, TRIAL_LATENCY, TRIAL_ERROR, 1, 2, 1);
data web.mathstro; set web.mathstro;
  Imathstro = IAT; Imathstro1 = IAT1; Imathstro2 = IAT2; MSexcl = subexcl; 
  MSmb3 = mb3; MSmb4 = mb4; MSmb6 = mb6; MSmb7 = mb7; 
  MSeb3 = eb3; MSeb4 = eb4; MSeb6 = eb6; MSeb7 = eb7;
  MSfb3 = fb3; MSfb4 = fb4; MSfb6 = fb6; MSfb7 = fb7;
  drop IAT IAT1 IAT2 subexcl mb3 mb4 mb6 mb7 eb3 eb4 eb6 eb7 fb3 fb4 fb6 fb7 diff1 diff2 as1 as2 cs1 cs2 fastm nb3 nb4 nb6 nb7;
DATA web.mathstro1; SET web.raw; 
  if TASK_NAME in ('Amathstroa', 'Amathstrob', 'Fmathstroa', 'Fmathstrob', 'Vmathstroa', 'Vmathstrob') then ; else delete;
  IF block IN ('B3','B6') THEN trial_number = trial_number+1; ELSE trial_number = trial_number+21;
      if block in ('B3','B4') AND 0 < trial_number < 16 THEN blockn = 'B3'; ELSE IF block in ('B3','B4') AND (trial_number > 15 AND trial_number < 31) THEN blockn = 'B4';
      ELSE IF block in ('B6', 'B7') AND 0 < trial_number < 16 THEN blockn = 'B6'; ELSE IF block in ('B6', 'B7') AND (trial_number > 15 AND trial_number < 31) THEN blockn = 'B7';
%iatCalc(web, web, mathstro1, mathstro1, blockn, session_id, trial_latency, trial_error, 1, 2, 1);
DATA web.mathstro1; SET web.mathstro1; Imathstroa = IAT1; Imathstrob = IAT2; KEEP session_id Imathstroa Imathstrob;
DATA web.mathstro2; SET web.raw;
  if TASK_NAME in ('Amathstroa', 'Amathstrob', 'Fmathstroa', 'Fmathstrob', 'Vmathstroa', 'Vmathstrob') then ; else delete;
  IF block IN ('B3','B6') THEN trial_number = trial_number+1; ELSE trial_number = trial_number+21;
      if block in ('B3','B4') AND 30 < trial_number < 46 THEN blockn = 'B3'; ELSE IF block in ('B3','B4') AND (trial_number > 45) THEN blockn = 'B4';
      ELSE IF block in ('B6', 'B7') AND 30 < trial_number < 46 THEN blockn = 'B6'; ELSE IF block in ('B6', 'B7') AND (trial_number > 45) THEN blockn = 'B7';
%iatCalc(web, web, mathstro2, mathstro2, blockn, session_id, trial_latency, trial_error, 1, 2, 1);
DATA web.mathstro2; SET web.mathstro2; Imathstroc= IAT1; Imathstrod= IAT2; KEEP session_id Imathstroc Imathstrod;
data web.mathstro; merge web.mathstro web.mathstro1 web.mathstro2; by session_id;

data web.Cattitudes; merge web.mathatt web.mathid web.mathanx web.genid web.mathstro; by session_id;

TITLE 'DATA = academic attitudes'; TITLE2 'IAT variables: initial';
ODS HTML BODY = 'C:\primary\DataWeb\RESEARCH.Studies\Mathbalance\datastorage\combine.07.IATmeans1.html';
  proc means data = web.Cattitudes; run;  *summary of variables generated by IAT macro;
ODS HTML CLOSE;

data temp; set web.raw;   
   format BLOCKa $8.;
   if TASK_NAME in ('Amathatta', 'Amathattb', 'Fmathatta', 'Fmathattb', 'Vmathatta', 'Vmathattb') then BLOCKa = ('att')||BLOCK;
   else if TASK_NAME in ('Amathida', 'Amathidb', 'Fmathida', 'Fmathidb', 'Vmathida', 'Vmathidb') then BLOCKa = ('id')||BLOCK;
   else if TASK_NAME in ('Amathanxa', 'Amathanxb', 'Fmathanxa', 'Fmathanxb', 'Vmathanxa', 'Vmathanxb') then BLOCKa = ('anx')||BLOCK; 
   else if TASK_NAME in ('genida', 'genidb') then BLOCKa = ('gen')||BLOCK; 
   else if TASK_NAME in ('Amathstroa', 'Amathstrob', 'Fmathstroa', 'Fmathstrob', 'Vmathstroa', 'Vmathstrob') then BLOCKa = ('stro')||BLOCK; 

   keep trial_number BLOCKa block_pairing_definition session_id TASK_NAME task_number;

   proc sort; by session_id BLOCKa;

	/*identify study condition*/
	data CONDatt; set temp; where trial_number = 1 and BLOCKa = 'attB3'; keep trial_number session_id BLOCKa TASK_NAME;
	proc transpose data=condatt name=name out=spdmeans; by session_id; var TASK_NAME;
	data CONDatt; set spdmeans(rename=(COL1=attTASK_NAME)); keep attTASK_NAME session_id; proc sort data=CONDatt;by session_id;run; 
    data CONDid; set temp; where trial_number = 1 and BLOCKa = 'idB3'; keep trial_number session_id BLOCKa TASK_NAME;
	proc transpose data=condid name=name out=spdmeans; by session_id; var TASK_NAME;
	data CONDid; set spdmeans(rename=(COL1=idTASK_NAME)); keep idTASK_NAME session_id; proc sort data=CONDid;by session_id;run; 
	data CONDanx; set temp; where trial_number = 1 and BLOCKa = 'anxB3'; keep trial_number session_id BLOCKa TASK_NAME;
	proc transpose data=condanx name=name out=spdmeans; by session_id; var TASK_NAME;
	data CONDanx; set spdmeans(rename=(COL1=anxTASK_NAME)); keep anxTASK_NAME session_id; proc sort data=CONDanx;by session_id;run; 
    data CONDgen; set temp; where trial_number = 1 and BLOCKa = 'genB3'; keep trial_number session_id BLOCKa TASK_NAME;
	proc transpose data=condgen name=name out=spdmeans; by session_id; var TASK_NAME;
	data CONDgen; set spdmeans(rename=(COL1=genTASK_NAME)); keep genTASK_NAME session_id; proc sort data=CONDgen;by session_id;run; 
	data CONDstro; set temp; where trial_number = 1 and BLOCKa = 'stroB3'; keep trial_number session_id BLOCKa TASK_NAME;
	proc transpose data=condstro name=name out=spdmeans; by session_id; var TASK_NAME;
	data CONDstro; set spdmeans(rename=(COL1=stroTASK_NAME)); keep stroTASK_NAME session_id; proc sort data=CONDstro;by session_id;run; 

	/*identify implicit task order*/
	data ORDatt; set temp; where trial_number = 1 and BLOCKa = 'attB3'; keep trial_number session_id BLOCKa task_number;
	proc transpose data=ORDatt name=name out=spdmeans; by session_id; var task_number;
	data ORDatt; set spdmeans(rename=(COL1=atttask_number)); keep atttask_number session_id; proc sort data=ORDatt;by session_id;run; 
    data ORDid; set temp; where trial_number = 1 and BLOCKa = 'idB3'; keep trial_number session_id BLOCKa task_number;
	proc transpose data=ORDid name=name out=spdmeans; by session_id; var task_number;
	data ORDid; set spdmeans(rename=(COL1=idtask_number)); keep idtask_number session_id; proc sort data=ORDid;by session_id;run; 
	data ORDanx; set temp; where trial_number = 1 and BLOCKa = 'anxB3'; keep trial_number session_id BLOCKa task_number;
	proc transpose data=ORDanx name=name out=spdmeans; by session_id; var task_number;
	data ORDanx; set spdmeans(rename=(COL1=anxtask_number)); keep anxtask_number session_id; proc sort data=ORDanx;by session_id;run; 
    data ORDgen; set temp; where trial_number = 1 and BLOCKa = 'genB3'; keep trial_number session_id BLOCKa task_number;
	proc transpose data=ORDgen name=name out=spdmeans; by session_id; var task_number;
	data ORDgen; set spdmeans(rename=(COL1=gentask_number)); keep gentask_number session_id; proc sort data=ORDgen;by session_id;run; 
	data ORDstro; set temp; where trial_number = 1 and BLOCKa = 'stroB3'; keep trial_number session_id BLOCKa task_number;
	proc transpose data=ORDstro name=name out=spdmeans; by session_id; var task_number;
	data ORDstro; set spdmeans(rename=(COL1=strotask_number)); keep strotask_number session_id; proc sort data=ORDstro;by session_id;run; 

	/*identify block pairing definitions*/
	data pairing; set temp; where trial_number = 1; keep session_id BLOCKa block_pairing_definition;
	proc transpose data=pairing prefix=P name=name out=pairing; by session_id; var block_pairing_definition;id BLOCKa;
	proc sort data=PAIRING; by session_id; run;

data COMBINE (drop=_LABEL_ name i); 
	merge condatt condid condanx condgen condstro
          ordatt ordid ordanx ordgen ordstro 
          pairing  web.cattitudes;
	by session_id;

proc means;run;

******
Bringing in the explicit data
******;
data temp; set web.acaexplicit;  format qrespons $50.;                 
     qrespons = question_response; drop question_response;
data temp; set temp; question_response = qrespons; drop qrespons;

*get rid of duplicate submissions to the dataset of the same data;
proc sort; by session_id question_name;  *sorting data;
data temp; set temp;
     repeat=0;  
     if session_id = lag(session_id) 
        and question_name = lag(question_name) then repeat=1; *if prior row is same as current row, mark as a repeat;
/**********          **********          **********          **********          **********          **********/
TITLE 'DATA = academic attitudes';
ODS HTML BODY = 'C:\primary\DataWeb\RESEARCH.Studies\Mathbalance\datastorage\combine.19.duplicatesRemovedExplicit.html';
  proc freq; tables repeat; run; 
ODS HTML CLOSE;
data temp; set temp;
     if repeat = 1 then delete; drop repeat;  *drop all repeat data;
*transpose data to get order-of-tasks variable;
proc transpose data=temp name=name out=explicit; 
               var task_number; by session_id;run;
data explicit; set explicit; Etasknum = COL1; keep session_id Etasknum;

*transpose data to get questions and answers;
proc transpose data=temp name=name out=explicit1; *variable name = question_name, data = question_response;
               var question_response; by session_id; id question_name;run; *one line per session;
data explicit1; set explicit1; drop name; 
data explicit1; merge explicit1 explicit; by session_id;
*explicit1 is a transposed data file, without yet being converted into usable (integer) data;

TITLE 'DATA = academic attitudes';
ODS HTML BODY = 'C:\primary\DataWeb\RESEARCH.Studies\Mathbalance\datastorage\combine.20.explicitVars2.html';
  proc contents data = explicit1; run; proc means; run; *summary of what is in the file;
ODS HTML CLOSE;
/* Note that most are character variables that will need conversion to numeric. */

*add column of repeated sessions from a single IP;
proc sort data=web.acasession; by session_id;  *sorting data;
data explicit1; set explicit1; exp=1;
data combine; set combine; imp=1;

*****combining iat, explicit, and session data into a single file*****;
data web.acatotal; merge explicit1 combine web.acasession; by session_id;
     if exp=1 or imp=1 then ; else delete; *empty sessions;

*cleaning up file by deleting bad data;
data web.acatotal; set web.acatotal;
     if session_id < 20000 then delete; /*pretesting*/ *drops 9 sessions;
     if participated = 'yes' then delete; *participated previously - drops 351 sessions;

     drop Sobeseatt Sobeseblf1 Sobeseblf2 Told Tyoung Achild1 Achild2 Achild3 Achild4 
          Aobeseid1 Aobeseid2 Aobeseid3 Aobeseid4 Aobeseid5 Aobeseid6 Sobeseatt 
          Drelatives aap1 aap7 ems1 ems4 ims3 ims4 att num T2 T3 participated;

data temp; set web.acatotal;
	 array subexcl (5) MAexcl MSexcl MXexcl MIexcl GIexcl;

     *identify possible exclusion criteria, but don't delete yet;
     if MAeb3 > .39 | MAeb4 > .39 | MAeb6 > .39 | MAeb7 > .39 then MAexcl = 3;
     else if MAfb3 > .24 | MAfb4 > .24 | MAfb6 > .24 | MAfb7 > .24 then MAexcl = 4;
     if MIeb3 > .39 | MIeb4 > .39 | MIeb6 > .39 | MIeb7 > .39 then MIexcl = 3;
     else if MIfb3 > .24 | MIfb4 > .24 | MIfb6 > .24 | MIfb7 > .24 then MIexcl = 4;
     if MXeb3 > .39 | MXeb4 > .39 | MXeb6 > .39 | MXeb7 > .39 then MXexcl = 3;
     else if MXfb3 > .24 | MXfb4 > .24 | MXfb6 > .24 | MXfb7 > .24 then MXexcl = 4;
     if GIeb3 > .39 | GIeb4 > .39 | GIeb6 > .39 | GIeb7 > .39 then GIexcl = 3;
     else if GIfb3 > .24 | GIfb4 > .24 | GIfb6 > .24 | GIfb7 > .24 then GIexcl = 4;
     if MSeb3 > .39 | MSeb4 > .39 | MSeb6 > .39 | MSeb7 > .39 then MSexcl = 3;
     else if MSfb3 > .24 | MSfb4 > .24 | MSfb6 > .24 | MSfb7 > .24 then MSexcl = 4;

	 Mdata = 0; Bdata = 0;
	 do i = 1 to 5;
	    if subexcl{i} in (2) then Mdata = Mdata+1;
		else if subexcl{i} in (1,3,4) then Bdata = Bdata+1;
	 end;

proc means; class Bdata; var session_id;run;
/*
            0    11588    11588        36015.40         9122.37        20003.00        51162.00
            1      566      566        36730.79         8604.63        20114.00        51145.00
            2      159      159        35985.64         9317.10        20117.00        51038.00
            3       96       96        36947.88         8812.80        20071.00        50996.00
*/

data temp; set temp;
    *drop participants who were excluded on 2 or more tasks;
    if Bdata > 1 then delete; *drops 255 who had 2 or more uninterpretable IATs suggesting bad behavior;

proc means; class Mdata; var session_id;run;
/*
            0    10656    10656        36043.04         9140.98        20003.00        51162.00
            1     1472     1472        36109.20         8793.46        20018.00        51161.00
            2       22       22        35756.50         9783.27        23059.00        48955.00
            3        4        4        30490.75         7309.90        21961.00        39202.00
*/

data temp; set temp;
   if MAexcl > 0 then do; Imathatt=.; Imathatt1=.; Imathatt2=.; Imathatta=.; Imathattb=.; Imathattc=.; Imathattd=.; end;
   if MIexcl > 0 then do; Imathid=.; Imathid1=.; Imathid2=.; Imathida=.; Imathidb=.; Imathidc=.; Imathidd=.; end;
   if MSexcl > 0 then do; Imathstro=.; Imathstro1=.; Imathstro2=.; Imathstroa=.; Imathstrob=.; Imathstroc=.; Imathstrod=.; end;
   if MXexcl > 0 then do; Imathanx=.; Imathanx1=.; Imathanx2=.; Imathanxa=.; Imathanxb=.; Imathanxc=.; Imathanxd=.; end;
   if GIexcl > 0 then do; Igenid=.; Igenid1=.; Igenid2=.; Igenida=.; Igenidb=.; Igenidc=.; Igenidd=.; end;


proc means; run;
proc contents;run;
proc means; class atttask_name; var Imathatt;run;
proc means; class idtask_name; var Imathid;run;
proc means; class anxtask_name; var Imathanx;run;
proc means; class strotask_name; var Imathstro;run;
proc means; class gentask_name; var Igenid;run;

data temp; set temp;
   if atttask_name in ("Amathattb", "Fmathattb", "Vmathattb") then do;
      Imathatt = 0-Imathatt; Imathatt1 = 0-Imathatt1; Imathatt2 = 0-Imathatt2;
      Imathatta = 0-Imathatta; Imathattb = 0-Imathattb; Imathattc = 0-Imathattc;
      Imathattd = 0-Imathattd;
   end;
   if idtask_name in ("Amathida", "Fmathida", "Vmathida", "Vmathidb") then do;
      Imathid = 0-Imathid; Imathid1 = 0-Imathid1; Imathid2 = 0-Imathid2;
      Imathida = 0-Imathida; Imathidb = 0-Imathidb; Imathidc = 0-Imathidc;
      Imathidd = 0-Imathidd;
   end;
   if strotask_name in ("Amathstroa", "Fmathstroa", "Vmathstroa") then do;
      Imathstro = 0-Imathstro; Imathstro1 = 0-Imathstro1; Imathstro2 = 0-Imathstro2;
      Imathstroa = 0-Imathstroa; Imathstrob = 0-Imathstrob; Imathstroc = 0-Imathstroc;
      Imathstrod = 0-Imathstrod;
   end;
   if anxtask_name in ("Amathanxb", "Fmathanxb", "Vmathanxb") then do;
      Imathanx = 0-Imathanx; Imathanx1 = 0-Imathanx1; Imathanx2 = 0-Imathanx2;
      Imathanxa = 0-Imathanxa; Imathanxb = 0-Imathanxb; Imathanxc = 0-Imathanxc;
      Imathanxd = 0-Imathanxd;
   end;
   if gentask_name in ("genidb") then do;
      Igenid = 0-Igenid; Igenid1 = 0-Igenid1; Igenid2 = 0-Igenid2;
      Igenida = 0-Igenida; Igenidb = 0-Igenidb; Igenidc = 0-Igenidc;
      Igenidd = 0-Igenidd;
   end;

  anx1 = mean(aanxiety1); anx2 = mean(aanxiety2);
  mid1 = mean(amathid1); mid2 = mean(amathid2); mid3 = mean(amathid3);
  part1 = mean(aparticipate1); part2 = mean(aparticipate2); part3 = mean(aparticipate3);
  gid1 = mean(asexid1); gid2 = mean(asexid2); 
  skill1 = mean(askills1); skill2 = mean(askills2);
  stro1 = mean(astro1); stro2 = mean(astro2);

  GRDyear = mean(Gdegree_year); /* missing data UGRDyear = mean(UDegree_year);*/
  ages = mean(age); education = mean(edu); eth = mean(ethnic); workyears = mean(working_years);
  format gender $1.; gender = sex;
  format IATexp $9.; IATexp = trim(num2);
  format nation $2.; nation = country;

  ACTEng = mean(ACT_English);  ACTMath = mean(ACT_Math);
  ACTRead = mean(ACT_Reading); ACTScience = mean(ACT_Science); 
  SATMath = mean(SAT_Math);    SATVerbal = mean(SAT_Verbal);

  Egenid = mean(Sgenid); Emathanx = mean(Smathanx);
  Emathatt = mean(Smathatt); Emathid = mean(Smathid);
  Emathstro = mean(Smathstro);

  Earts = mean(Tarts); Efurniture = mean(Tfurniture);
  Elanguage = mean(Tlanguage); Emath = mean(Tmath);

  drop aanxiety1 aanxiety2 amathid1 amathid2 amathid3 aparticipate1 aparticipate2 aparticipate3 
       asexid1 asexid2 askills1 askills2 astro1 astro2 Gdegree_year UDegree_year age edu 
       ethnic working_years sex num2 country ACT_English ACT_Math ACT_Reading ACT_Science
       SAT_Math SAT_Verbal Sgenid Smathanx Smathid Smathatt Smathstro
       Tarts Tlanguage Tfurniture Tmath occupation;

   if umajor1 in ('Math', 'Engineering', 'Natural Sciences', 'Information Technology') then maj = 'Science';
   else if umajor1 in ('Humanities', 'Fine Arts', 'Languages') then maj = 'Human';
   else if umajor1 in ('.',"", "None") then maj = '.';
   else maj = 'other';

   if umajor2 in ('Math', 'Engineering', 'Natural Sciences', 'Information Technology') then maj2 = 'Science';
   else if umajor2 in ('Humanities', 'Fine Arts', 'Languages') then maj2 = 'Human';
   else if umajor2 in ('.',"", "None") then maj2 = '.';
   else maj2 = 'other';

   if gmajor1 in ('Math', 'Engineering', 'Natural Sciences', 'Information Technology') then gmaj = 'Science';
   else if gmajor1 in ('Humanities', 'Fine Arts', 'Languages') then gmaj = 'Human';
   else if gmajor1 in ('.',"", "None") then gmaj = '.';
   else gmaj = 'other';

proc contents;run;

data web.acafinal; set temp; run;

TITLE 'DATA = academic attitudes';
ODS HTML BODY = 'C:\primary\DataWeb\RESEARCH.Studies\Mathbalance\datastorage\combine.25.creationDateMonYr.html';
  proc freq data=web.racedemo;format creation_date dtmonyy5.;tables creation_date;run;
ODS HTML CLOSE;



